/*
 * Created on Jul 18, 2005
 *
 */
package org.mushroomdb.interceptor;

import java.lang.reflect.InvocationTargetException;

import org.apache.log4j.Logger;
import org.mushroomdb.util.Properties;
import org.mushroomdb.util.PropertiesHelper;


/**
 * @author Matu
 *
 */
public class InterceptorService {
	private static InterceptorService instanceReference;
	private Interceptor interceptor;
	//TODO SACAR ESTO
	private CompositeInterceptor compositeInterceptor;
	private Logger logger;

	/**
	 * Constructor privado.
	 */
	private InterceptorService() {
		this.logger = Logger.getLogger(this.getClass());
	}

	/**
	 * Construye el interceptor.
	 */
	private void loadInterceptor() {
		CompositeInterceptor compositeInterceptor = new CompositeInterceptor();
		
		int index = 1;
		String interceptorClass = null;
		do {
			interceptorClass = Properties.getProperty(PropertiesHelper.INTERCEPTOR + "." + index); //$NON-NLS-1$
			if (interceptorClass != null) {
				Interceptor interceptor = this.getInterceptorInstance(interceptorClass);
				if (interceptor != null) {
					compositeInterceptor.addInterceptor(interceptor);
					logger.info("Interceptor " + interceptorClass + " registered."); //$NON-NLS-1$ //$NON-NLS-2$
				} else {
					logger.warn("Interceptor " + interceptorClass + " was not registered."); //$NON-NLS-1$ //$NON-NLS-2$
				}
			}
			index++;
		} while (interceptorClass != null);
		
		this.interceptor = compositeInterceptor;
		
	}
	
	/**
	 * Obtiene la instancia de interceptor a partir del nombre de su clase.
	 * @param className
	 * @return
	 */
	public Interceptor getInterceptorInstance(String className) {
		try {
			Class clazz = Class.forName(className);
			return (Interceptor) clazz.getConstructor(new Class[] {}).newInstance(new Object[] {});
		} catch (ClassNotFoundException e) {
			this.logger.error(className,e);
			return null;
		} catch (SecurityException e) {
			this.logger.error(className,e);
			return null;
		} catch (NoSuchMethodException e) {
			this.logger.error(className,e);
			return null;
		} catch (IllegalArgumentException e) {
			this.logger.error(className,e);
			return null;
		} catch (InstantiationException e) {
			this.logger.error(className,e);
			return null;
		} catch (IllegalAccessException e) {
			this.logger.error(className,e);
			return null;
		} catch (InvocationTargetException e) {
			this.logger.error(className,e);
			return null;
		}
	}

	/**
	 * Devuelve el InterceptorService.
	 * @return
	 */
	public static InterceptorService getInstance() {
		if (InterceptorService.instanceReference == null) {
			InterceptorService.instanceReference = new InterceptorService();
		}
		return InterceptorService.instanceReference;
	}
	
	/**
	 * Devuelve el interceptor.
	 * @return
	 */
	public Interceptor getInterceptor() {
		if (this.interceptor == null) {
			this.loadInterceptor();
		}
		return this.interceptor;
	}
	
	//TODO SACAR ESTO
	public CompositeInterceptor getCompositeInterceptor() {
		return this.compositeInterceptor;
	}
}
